package com.itqf.service;

import com.itqf.client.TbItemFeignClient;
import com.itqf.entity.TbItem;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/12/24
 * @Time: 下午2:23
 */
@Service
public class TbItemService {

    @Resource
    private TbItemFeignClient client;



    //1)服务降级 fallbackMethod 方法名称
    //2)线程隔离
    //   HystrixCommandProperties
    @HystrixCommand(fallbackMethod = "find1Fallback",commandProperties =
            {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000"),
                    @HystrixProperty(name = "execution.isolation.strategy",value ="THREAD"),
                    @HystrixProperty(name="circuitBreaker.enabled",value = "true"),
                    @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "10"),
                    @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50"),} )

    public   TbItem  find(){
        System.out.println("find--thread"+Thread.currentThread().getName());
        int i = 10/0;
        TbItem tbItem = client.findAll();

        return tbItem;
    }

    //
    //1)服务降级 fallbackMethod 方法名称
    //2)线程隔离
    //HystrixCommandProperties
    @HystrixCommand(fallbackMethod = "find1Fallback",commandProperties =
            {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000"),
                    @HystrixProperty(name = "execution.isolation.strategy",value ="SEMAPHORE"),
                    @HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests",value = "2")} )
    @CacheResult
    public   TbItem  find1(){
        System.out.println("find1--thread:-->"+Thread.currentThread().getName());

        TbItem tbItem = client.findAll();

        return tbItem;
    }

    public  TbItem find1Fallback(){
        System.out.println("find1Fallback.......");
        TbItem tbItem = new TbItem();
        tbItem.setId(-1);
        tbItem.setTitle("服务器忙，请稍后再试....");
        return tbItem;
    }

    //1)服务降级 fallbackMethod 方法名称
    //2)线程隔离
    //   HystrixCommandProperties
    @HystrixCommand(fallbackMethod = "findFallback",commandProperties =
            {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000"),
                    @HystrixProperty(name = "execution.isolation.strategy",value ="THREAD"),
                    @HystrixProperty(name="circuitBreaker.enabled",value = "true"),
                    @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "10"),
                    @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50"),} )
    @CacheResult() //缓存该方法相同的参数 结果
    //findById:1 : {id:1,titel:}
    public   TbItem  findById(@CacheKey int  id){
        System.out.println("findById--thread"+Thread.currentThread().getName());

        TbItem tbItem = client.findById(id);

        return tbItem;
    }

    @HystrixCommand(commandProperties =
            {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000"),
                    @HystrixProperty(name = "execution.isolation.strategy",value ="THREAD"),
                    @HystrixProperty(name="circuitBreaker.enabled",value = "true"),
                    @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "10"),
                    @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50"),} )
    @CacheRemove(commandKey = "findById") //清除---缓存该方法相同的参数 结果
    public   void  clearfindById(@CacheKey int  id){
        System.out.println("clearfindById--thread"+Thread.currentThread().getName());
        System.out.println("clearfindById  清空缓存！！！");
    }


    public   TbItem  findFallback(int id){
        System.out.println("findFallback.......");
        TbItem tbItem = new TbItem();
        tbItem.setId(-1);
        tbItem.setTitle("服务器忙，请稍后再试....");
        return tbItem;
    }



}
